package client;

import java.io.ObjectOutputStream;
import javax.swing.SwingWorker;
import network.Message;
import network.OpCode;
import ui.BuddyEntry;
import ui.GroupEntry;

public class SendMessage {


	private static ObjectOutputStream out = null;

	
	public static void setObjectOutput(ObjectOutputStream o){
		out = o;
	}

	public static boolean sendBuddyMessage(final String text, final BuddyEntry buddy) {
		try {
			System.out.println(" --- SendMessage (sendMessage)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.MESSAGE, buddy.getName(), text));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (sendMessage)" + e.getMessage());
			return false;
		}
		return true;
	}
	
	public static boolean sendGroupMessage(final String text, final String group, final String username) {
		try {
			System.out.println(" --- SendMessage (sendMessage)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.MESSAGE, group, text));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (sendMessage)" + e.getMessage());
			return false;
		}
		return true;
	}

	public static boolean addBuddy(final BuddyEntry buddy) {
		try {
			System.out.println(" --- SendMessage (addBuddy)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.ADDBUDDY, buddy.getName(), null));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (addBuddy)" + e.getMessage());
			return false;
		}
		return true;
	}

	public static boolean removeBuddy(final BuddyEntry buddy) {
		try {
			System.out.println(" --- SendMessage (removeBuddy)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.DELBUDDY, buddy.getName(), null));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (removeBuddy)" + e.getMessage());
			return false;
		}
		return true;
	}
	
	public static boolean removeGroup(final String groupName) {
		try {
			System.out.println(groupName + "---");
			System.out.println(" --- SendMessage (removeGroup)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.DELGROUP, groupName, null));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (removeGroup)" + e.getMessage());
			return false;
		}
		return true;
	}

	public static boolean addMember(final GroupEntry group, final BuddyEntry buddy) {
		try {
			System.out.println(" --- SendMessage (addMember)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.GROUPADD, buddy.getName(), group.getName()));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (addMember)" + e.getMessage());
			return false;
		}
		return true;
	}

	public static boolean buddyAccept(final BuddyEntry buddy) {
		try {
			System.out.println(" --- SendMessage (buddyAccept)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.BUDDYACCEPT, buddy.getName(), null));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (buddyAccept)" + e.getMessage());
			return false;
		}
		return true;
	}

	public static boolean validateLogin(final String username, final String password) {

		try {
			System.out.println(" --- SendMessage (validateLogin)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.LOGIN, username, password));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (validateLogin)" + e.getMessage());
			return false;
		}
		return true;
	}
	
	public static boolean getBuddies() {

		try {
			System.out.println(" --- SendMessage (getBuddies)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.GETBUDDIES, null, null));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (getBuddies)" + e.getMessage());
			return false;
		}
		return true;
	}
	
	public static boolean getGroups() {

		try {
			System.out.println(" --- SendMessage (getGroups)");
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				
				@Override
				protected Void doInBackground() throws Exception {
					out.writeObject(new Message(OpCode.GETGROUPS, null, null));
					return null;
				}
			};
			worker.execute();
		} catch (Exception e) {
			System.out.println(" [x] SendMessage (getGroups)" + e.getMessage());
			return false;
		}
		return true;
	}

}